package DAO;

import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import Data.IocThongtintiepnhanMucdonghiemtrong;
import HibernateUtil.HibernateUtil;

public class ioc_thongtintiepnhan_mucdonghiemtrongDAO {
	private Session session;
	public ioc_thongtintiepnhan_mucdonghiemtrongDAO() {
		try {
			this.session = HibernateUtil.getSessionFactory().openSession();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean save(IocThongtintiepnhanMucdonghiemtrong tt){
		boolean check = false;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(tt);
			transaction.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			try {
				if (transaction.wasCommitted()) {
					transaction.rollback();
				}
			} catch (HibernateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return check;
	}
	public boolean edit(IocThongtintiepnhanMucdonghiemtrong tt){
		boolean check = false;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(tt);
			transaction.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			try {
				if (transaction.wasCommitted()) {
					transaction.rollback();
				}
			} catch (HibernateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return check;
	}
	public boolean delete(IocThongtintiepnhanMucdonghiemtrong tt){
		boolean check = false;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(tt);
			transaction.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			try {
				if (transaction.wasCommitted()) {
					transaction.rollback();
				}
			} catch (HibernateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return check;
	}
	public IocThongtintiepnhanMucdonghiemtrong[] getAll(){
		Transaction transaction = null;
		try {
            transaction = session.beginTransaction();
            String sql = "from IocThongtintiepnhanMucdonghiemtrong";
            Query query = session.createQuery(sql);
            List list = query.list();
            IocThongtintiepnhanMucdonghiemtrong[] loc = new IocThongtintiepnhanMucdonghiemtrong[list.size()];
            list.toArray(loc);
            session.flush();
            transaction.commit();
            return loc;
        } catch (Exception e) {
            try {
				if (transaction.wasCommitted()) {
				    transaction.rollback();
				}
			} catch (HibernateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            e.printStackTrace();
        }
        return null;
	}
	public IocThongtintiepnhanMucdonghiemtrong searchById(int id){
		IocThongtintiepnhanMucdonghiemtrong obj = null;
		Transaction transaction = null;
		try {
            transaction = session.beginTransaction();
            String sql = "from IocThongtintiepnhanMucdonghiemtrong where id = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            obj = (IocThongtintiepnhanMucdonghiemtrong) query.uniqueResult();
            session.flush();
            transaction.commit();
        } catch (Exception e) {
            try {
				if (transaction.wasCommitted()) {
				    transaction.rollback();
				}
			} catch (HibernateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            e.printStackTrace();
        }
        return obj;
	}

}
